import * as ThreeComm from './three-comm.js';
export class Drager {
  scene;
  camera;
  planeMesh;
  renderer;
  raycaster;
  pointer;
  orbitControls;
  dragControls;
  mouseSceneState;
  gragModle;  // boolean
  curSelectedMess;
  faceIndex;
  callBack;
  lastMousePosition;  // 记录鼠标按下时候的位置

  constructor(scene, camera, planeMesh, renderer, raycaster, pointer, orbitControls, dragControls, mouseSceneState) {
    this.scene = scene;
    this.camera = camera;
    this.planeMesh = planeMesh
    this.renderer = renderer;
    this.raycaster = raycaster;
    this.pointer = pointer;
    this.orbitControls = orbitControls;
    this.dragControls = dragControls;
    this.mouseSceneState = mouseSceneState
  }

  regHover() {
    const onSceneMouseMove = (event) => {
      this.pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
      this.pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
      this.raycaster.setFromCamera(this.pointer, this.camera);
      // 计算物体和射线的焦点
      var intersects = this.raycaster.intersectObjects(this.scene.children);

      if(this.gragModle){
        this.pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
        this.pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
        this.raycaster.setFromCamera(this.pointer, this.camera);
        // 计算物体和射线的焦点
        const plane = this.raycaster.intersectObject(this.planeMesh);
        if (plane.length > 0) {
          // 获取交点的世界坐标
          const currentMousePosition = plane[0].point;
          // 计算移动距离
          const disX = currentMousePosition.x - this.lastMousePosition.x
          const disY = currentMousePosition.y - this.lastMousePosition.y
          const disZ = currentMousePosition.z - this.lastMousePosition.z

          let opt = {
          }

          switch (this.faceIndex) {
            case 0:
            case 1:
              opt = {
                z: disZ
              }
              break;
            case 2:
            case 3:
              opt = {
                y: disY
              }
              break;
            case 4:
            case 5:
              opt = {
                x: disX
              }
              break;
          }
          this.callBack.call(null, this.curSelectedMess, this.faceIndex, opt)
          this.curSelectedMess.object.updateMatrix();
          this.camera.updateMatrixWorld();
          this.planeMesh.updateMatrixWorld();
          this.lastMousePosition.copy(currentMousePosition);  // 更新点
        }
      }else{
        if (intersects.length > 0) {
          let intersect = intersects[0]
          let object3d = intersect.object
          if (object3d.userData && object3d.userData.type == 'dragmarker') {
            
            let faceindex = object3d.userData.faceindex
            if(faceindex ==0 || faceindex == 1){
              this.planeMesh.rotation.x = 0.5 * Math.PI;
            }else{
              this.planeMesh.rotation.x = 0
            }

            this.dragControls.enabled = false
            // this.dragControls.disconnect()
          } else {
            this.dragControls.enabled = true
          }
        }
      }

    }

    window.addEventListener('mousemove', onSceneMouseMove, false);
  }

  //
  callDrag(mesh, event, callback) {
    this.pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
    this.pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
    this.raycaster.setFromCamera(this.pointer, this.camera);
    // 计算物体和射线的焦点
    var intersects = this.raycaster.intersectObjects(this.scene.children);

    if (intersects.length > 0) {
      let intersect = intersects[0]
      let object3d = intersect.object
      if (object3d.userData && object3d.userData.type == 'dragmarker') {
        const faceIndex = object3d.userData.faceindex
        const grandParent = {object: object3d.parent.parent}
        this.gragModle = true
        this.orbitControls.enabled = false
        this.curSelectedMess = grandParent // mesh//grandParent
        this.callBack = callback
        this.faceIndex = faceIndex

        this.lastMousePosition = this.getPoinperPosition(event)

      }
    }
  }

  // 释放
  release(){
    this.gragModle = false
    this.orbitControls.enabled = true
    this.curSelectedMess = null
    this.callBack = null
  }

  // 获取场景点坐标
  getPoinperPosition(event){
    this.pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
    this.pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
    this.raycaster.setFromCamera(this.pointer, this.camera);
    // 计算物体和射线的焦点
    const plane = this.raycaster.intersectObject(this.planeMesh);
    if (plane.length > 0) {
      return plane[0].point
    }
    return null
  }
}
